function GetID(ID) {
  if (document.all) {
 //	  alert(ID + " - (document.all[ID]) " + document.all[ID]);
    return document.all[ID];
  } else if (document.getElementById) {
// 	  alert(ID + " - (document.getElementById(ID)) " + document.getElementById(ID));
    return document.getElementById(ID);
  } else {
//	  alert(ID + " - (undefined) " + undefined);
    return undefined;
  }
}

function MoveAddrsTo(Suburb, PostCode, StateCode, QuickList) {
  var QL = GetID(QuickList); if (!QL) { return false; }
  var FSuburb = GetID(Suburb); if (!FSuburb) { return false; }
  var FPostCode = GetID(PostCode); if (!FPostCode) { return false; }
  var FStateCode = GetID(StateCode); if (!StateCode) { return false; }
//  var FCountry = GetID(Country); if (!Country) { return; }

  var re = new RegExp('(^\\d+),(.*),(.*)$');

  var QLO = QL.options;
  for (var i = 0, k = 0; i < QLO.length && k < 100; k++) {
    if (QLO[i].selected) {
      var Matches = re.exec(QLO[i].value);
      if (Matches[0]) {
//        FCountry.value =  RegExp.$4;
        FStateCode.value =  RegExp.$3;
        FSuburb.value =  RegExp.$2;
        FPostCode.value =  RegExp.$1;
      }
      //QLO[i] = null;
    } else {
      i++;
    }
  }
  return false;
}


function MoveSTCTo(StreetTypeCode, QuickList) {
  var QL = GetID(QuickList); if (!QL) { return false; }
  var FStreetTypeCode = GetID(StreetTypeCode); if (!StreetTypeCode) { return false; }

  var re = new RegExp('(^\\d+),(.*),(.*)$');

  var QLO = QL.options;
  for (var i = 0, k = 0; i < QLO.length && k < 100; k++) {
    if (QLO[i].selected) {
	alert ("Place "+QLO[i].value+" in streetTypeCode drop down");
    FStreetTypeCode.selected =  QLO[i].value;

//	  var Matches = re.exec(QLO[i].value);
//    if (Matches[0]) {
//        FStreetTypeCode.value =  RegExp.$1;
//      }
      //QLO[i] = null;
    } else {
      i++;
    }
  }
  return false;
}


function uncheckCheckBox(me, op, all)
{ 
//  var me_o = GetID(me); if (!me_o) { return; }
  var object = GetID(op); if (!object) { return; }
		object.checked = false;
  var all_o = GetID(all); if (!all_o) { return; }
		all_o.checked = false;
//alert(eval(myname + ".checked"));
//	var object = eval(myname);

///	alert("About to set objects to false");
}

function openUpdateSql(mySQL, PHPSESSID)
{ 
	random_num = (Math.round((Math.random()*9)+1))
	window.open ("Update_sql.php?fixSQL="+mySQL+"&PHPSESSID="+PHPSESSID, 'newwindow'+random_num, 	config='height=120, width=300, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no')
	//config='height=800, width=800, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, directories=yes, status=yes')

}

function openReport(report, link, message, PHPSESSID)
{ 
	random_num = (Math.round((Math.random()*9)+1))
	window.open ("index.php?view=Report_open&report="+report+"&link="+link+"&message="+message+"&PHPSESSID="+PHPSESSID, 'newwindow'+random_num,'height=420, width=600, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, directories=yes, status=yes');
	//config='height=800, width=800, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, directories=yes, status=yes')
}


function setFocus(object,textValue, abc){
var FStreetTypeCode;
  var QLO = abc.options;
  for (var i = 0, k = 0; i < QLO.length && k < 100; k++) {
    if (QLO[i].selected) {
//	alert ("Place "+QLO[i].value+" in streetTypeCode drop down");
    FStreetTypeCode =  QLO[i].value;
	break;
    } else {
      i++;
    }
  }
	newIndex = FStreetTypeCode;

	newIndex=(textValue)?newIndex+"":parseFloat(newIndex)
	var size=object.options.length
	var noMatch=0;
	if(textValue){//find text or value?
		switch (textValue){
			case 1://search values
			for(var t=0;t<size;t++){
				if(object.options[t].value==newIndex){newIndex=t;noMatch=1;break}
			}
			break;
			case 2://search texts
			for(var t2=0;t2<size;t2++){
				if(object.options[t2].text==newIndex){newIndex=t2;noMatch=1;break}
			}
		}  // end switch
	}
	else{
        noMatch=1
    }
	if(!noMatch||newIndex>size-1){return false;}
	var current=0
	for(var c=0;c<size;c++){//no selection?
		if(object.options[c].selected){current=c;break;}
	}
	var texts=new Array(size)
	var values=new Array(size)
	for(var i1=0;i1<size;i1++){
		texts[i1]=object.options[i1].text;
		values[i1]=object.options[i1].value;
	}
	for(var i2=0;;i2++){
		if(i2==size){return false;}
		object.options[current].text=texts[newIndex]
		object.options[current].value=values[newIndex]
		newIndex=(newIndex==size-1)?0:++newIndex;
		current=(current==size-1)?0:++current
	}
	/*keep this comment to reuse freely
	http://www.unitedscripters.com */
}

function setFocusT(object,textValue, abc){
	var FStreetTypeCode;
	var QLO = abc.options;
	for (var i = 0, k = 0; i < QLO.length && k < 100; k++) {
		if (QLO[i].selected) {
			break;
		} else {
			i++;
		}
	}
	object.value = QLO[i].value;
}

function tE(e, form) {
// trapEnter

//alert(e.keyCode);
	 if (e.keyCode && e.keyCode != 13) return true;
     else if (e.which && e.which != 13) return true;
//     var field, msg = '';
//     if (!form.username.value) {
//         field = form.username;
//         msg = 'Please enter your username.'
//   } else if (!form.password.value) {
//         field = form.password;
//        msg = 'Please enter your password.'
//      }
//     if (msg) {
//         alert(msg);
//         if (field) field.focus();
//         return false;
//   }
//alert(form.submitData.value);
		form.submitData.click();
//	 form.submitData.value = "Update information";
//	 form.submitData("Update information");
return true;
}

function cf() {
	return confirm('Are you sure you wish to delete this Individual?');
}
function ca() {
	return confirm('Are you sure ? This may take some time...');
}

function cq() {
	return confirm('Are you sure you want to delete this family and ALL individuals?');
}

/////////////////
// from
// http://www.harrymaugans.com/2007/03/05/how-to-create-a-collapsible-div-with-javascript-and-css/
/////////////////

  function toggleDiv(divid){
    if(document.getElementById(divid).style.display == 'none' || document.getElementById(divid).style.display == '' || document.getElementById(divid).style.display == null){
      document.getElementById(divid).style.display = 'block';
    }else{
      document.getElementById(divid).style.display = 'none';
    }
  }
/////////////////


// Fix links that need to open in new window
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
    function setObjfocus(a_field_id) {
        $(a_field_id).focus();
    }


window.onload = externalLinks;
